package com.Dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.database.Mysql;
import com.news.model.Comment;

import java.util.ArrayList;
import java.util.List;

public class CommentDao {
    private SQLiteDatabase db;
    private Mysql dbHelper;

    public CommentDao(Context context) {
        dbHelper = new Mysql(context);
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        db.close();
    }

    //插入评论
    public long insertNews(Comment comment) {
        ContentValues values = new ContentValues();
        values.put("newsid", comment.getNewsId());
        values.put("userid", comment.getUserId());
        values.put("content", comment.getContent());
        values.put("created_at", comment.getCreatedAt());
        values.put("username",comment.getUserName());
        values.put("parentid", comment.getParentId() == null ? null : comment.getParentId());
        return db.insert("comments", null, values);
    }

    // 获取所有评论
    public List<Comment> getAllComments() {
        List<Comment> commentList = new ArrayList<>();
        Cursor cursor = null;
        try {
            cursor = db.query("comments", null, null, null, null, null, null);

            if (cursor != null && cursor.moveToFirst()) {
                do {
                    Comment comment = new Comment();

                    int commentIdIndex = cursor.getColumnIndex("commentid");
                    if (commentIdIndex != -1) {
                        comment.setCommentId(cursor.getInt(commentIdIndex));
                    } else {
                        Log.e("Database", "Column commentid not found");
                    }

                    int newsIdIndex = cursor.getColumnIndex("newsid");
                    if (newsIdIndex != -1) {
                        comment.setNewsId(cursor.getInt(newsIdIndex));
                    } else {
                        Log.e("Database", "Column newsId not found");
                    }

                    int userIdIndex = cursor.getColumnIndex("userid");
                    if (userIdIndex != -1) {
                        comment.setUserId(cursor.getInt(userIdIndex));
                    } else {
                        Log.e("Database", "Column userId not found");
                    }

                    int contentIndex = cursor.getColumnIndex("content");
                    if (contentIndex != -1) {
                        comment.setContent(cursor.getString(contentIndex));
                    } else {
                        Log.e("Database", "Column content not found");
                    }

                    int createdAtIndex = cursor.getColumnIndex("created_at");
                    if (createdAtIndex != -1) {
                        comment.setCreatedAt(cursor.getLong(createdAtIndex));
                    } else {
                        Log.e("Database", "Column createdAt not found");
                    }

                    int parentIdIndex = cursor.getColumnIndex("parentid");
                    if (parentIdIndex != -1) {
                        comment.setParentId(cursor.getInt(parentIdIndex));
                    } else {
                        Log.e("Database", "Column parentId not found");
                    }
                    int usernameIndex = cursor.getColumnIndex("username");
                    if(usernameIndex!=-1){
                        comment.setUserName(cursor.getString(usernameIndex));
                    }else {
                        Log.e("Database", "Column username not found");
                    }
                    Log.e("所有查询","succeed");
                    commentList.add(comment);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            Log.e("Database", "Error fetching comments", e);
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return commentList;
    }

    //
    //根据newsId获取评论
    public List<Comment> getCommentsByNewsId(int newsId){
        List<Comment> commentList = new ArrayList<>();
        Cursor cursor = null;
        try{
            cursor = db.query("comments", null, "newsid = ?", new String[]{String.valueOf(newsId)}, null, null, null);
            if(cursor != null && cursor.moveToFirst()){
                do{
                    Comment comment = new Comment();

                    int commentIdIndex = cursor.getColumnIndex("commentid");
                    if(commentIdIndex != -1){
                        comment.setCommentId(cursor.getInt(commentIdIndex));
                    }

                    int newsIdIndex = cursor.getColumnIndex("newsid");
                    if(newsIdIndex != -1){
                        comment.setNewsId(cursor.getInt(newsIdIndex));
                    }

                    int userIdIndex = cursor.getColumnIndex("userid");
                    if (userIdIndex != -1) {
                        comment.setUserId(cursor.getInt(userIdIndex));
                    }

                    int contentIndex = cursor.getColumnIndex("content");
                    if (contentIndex != -1) {
                        comment.setContent(cursor.getString(contentIndex));
                    }

                    int createdAtIndex = cursor.getColumnIndex("created_at");
                    if (createdAtIndex != -1) {
                        comment.setCreatedAt(cursor.getLong(createdAtIndex));
                    }

                    int parentIdIndex = cursor.getColumnIndex("parentid");
                    if (parentIdIndex != -1) {
                        comment.setParentId(cursor.getInt(parentIdIndex));
                    }

                    int usernameIndex = cursor.getColumnIndex("username");
                    if(usernameIndex!=-1){
                        comment.setUserName(cursor.getString(usernameIndex));
                    }else {
                        Log.e("Database", "Column username not found");
                    }

                    Log.e("id查询","succeed");
                    commentList.add(comment);
                } while(cursor.moveToNext());
            }
        } catch (Exception e){
            Log.e("Database", "Error fetching comments by newsid", e);
        } finally {
            {
                if(cursor != null && !cursor.isClosed()){
                    cursor.close();
                }
            }
            return  commentList;
        }
    }
    //根据userid 获取 新闻id
    public List<Comment> getCommentsByUserid(int userid){
        List<Comment> commentList = new ArrayList<>();
        Cursor cursor = null;
        try{
            cursor = db.query("comments", null, "userid = ?", new String[]{String.valueOf(userid)}, null, null, null);
            if(cursor != null && cursor.moveToFirst()){
                do{
                    Comment comment = new Comment();

                    int commentIdIndex = cursor.getColumnIndex("commentid");
                    if(commentIdIndex != -1){
                        comment.setCommentId(cursor.getInt(commentIdIndex));
                    }

                    int newsIdIndex = cursor.getColumnIndex("newsid");
                    if(newsIdIndex != -1){
                        comment.setNewsId(cursor.getInt(newsIdIndex));
                    }

                    int userIdIndex = cursor.getColumnIndex("userid");
                    if (userIdIndex != -1) {
                        comment.setUserId(cursor.getInt(userIdIndex));
                    }

                    int contentIndex = cursor.getColumnIndex("content");
                    if (contentIndex != -1) {
                        comment.setContent(cursor.getString(contentIndex));
                    }

                    int createdAtIndex = cursor.getColumnIndex("created_at");
                    if (createdAtIndex != -1) {
                        comment.setCreatedAt(cursor.getLong(createdAtIndex));
                    }

                    int parentIdIndex = cursor.getColumnIndex("parentid");
                    if (parentIdIndex != -1) {
                        comment.setParentId(cursor.getInt(parentIdIndex));
                    }

                    int usernameIndex = cursor.getColumnIndex("username");
                    if(usernameIndex!=-1){
                        comment.setUserName(cursor.getString(usernameIndex));
                    }else {
                        Log.e("Database", "Column username not found");
                    }

                    Log.e("id查询","succeed");
                    commentList.add(comment);
                } while(cursor.moveToNext());
            }
        } catch (Exception e){
            Log.e("Database", "Error fetching comments by newsid", e);
        } finally {
            {
                if(cursor != null && !cursor.isClosed()){
                    cursor.close();
                }
            }
            return  commentList;
        }
    }
    // 删除评论（根据commentId）
    public void deleteComment(int commentId) {
        db.delete("comments", "commentid = ?", new String[]{String.valueOf(commentId)});
    }
}
